package com.educate.device.rpc.provide.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.educate.common.utils.converter.ListUtils;
import com.educate.common.utils.exception.AppException;
import com.educate.common.web.BeanUtils;
import com.educate.device.dao.DeviceDao;
import com.educate.device.dao.DeviceMessageDao;
import com.educate.device.dao.DeviceStatusDao;
import com.educate.device.model.bo.DeviceBO;
import com.educate.device.model.bo.DeviceMessageBO;
import com.educate.device.model.bo.DeviceStatusBO;
import com.educate.device.model.po.Device;
import com.educate.device.model.po.DeviceMessage;
import com.educate.device.rpc.api.DeviceService;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Log4j2
@Service(
        version = "${api.service.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)
public class DeviceServiceImpl implements DeviceService {

    @Resource DeviceDao deviceDao;
    @Resource DeviceMessageDao deviceMessageDao;
    @Resource DeviceStatusDao deviceStatusDao;

    @Override
    public void create(DeviceBO bo) {
        deviceDao.insert(bo);
    }

    @Override
    public void delete(DeviceBO bo) {
        deviceDao.delete(bo);
    }

    @Override
    public void delete(String deviceId) {
        deviceDao.deleteById(deviceId);
    }

    @Override
    public DeviceBO detail(String deviceId) {
        Optional<Device> optional = deviceDao.findById(deviceId);
        if (!optional.isPresent()) {
            throw new AppException("不存在的实例");
        }
        return new DeviceBO(optional.get());
    }

    @Override
    public DeviceBO detail(DeviceBO bo) {
//        return detail(bo.getOrganizationId());
        return null;
    }

    @Override
    public void update(DeviceBO bo) {
        Optional<Device> optional = deviceDao.findById(bo.getDeviceId());
        if (!optional.isPresent()) {
            throw new AppException("不存在的实例");
        }
        BeanUtils.copyPropertiesNonEmpty(bo, optional.get());
        deviceDao.save(optional.get());
    }

    @Override
    public Page<DeviceBO> query(DeviceBO bo, PageRequest pageRequest) {
        return deviceDao.query(bo, pageRequest).map(DeviceBO::new);
    }

    @Override
    public List<DeviceBO> list(DeviceBO bo) {
        List<Device> result = deviceDao.findAll();
        return ListUtils.map(result, DeviceBO::new);
    }

    @Override
    public DeviceMessageBO resultOperate(DeviceMessageBO bo) {
        Optional<DeviceMessage> deviceMessageOptional = deviceMessageDao.findByDeviceIdAndMessageId(bo.getDeviceId(),
                bo.getMessageId());
        return deviceMessageOptional.map(DeviceMessageBO::new)
                .orElse(null);
    }

    @Override
    public DeviceStatusBO state(DeviceBO bo) {
        return deviceStatusDao.findById(bo.getDeviceId())
                .map(DeviceStatusBO::new)
                .orElse(null);
    }
}
